package cn.wang.service.impl;

import cn.wang.common.Exceptions.impl.LyException;
import cn.wang.common.enums.impl.ExceptionEnum;
import cn.wang.common.vo.PageResult;
import cn.wang.mapper.IBrandMapper;
import cn.wang.pojo.Brand;
import cn.wang.pojo.dto.QueryBrandDTO;
import cn.wang.service.IBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    IBrandMapper brandMapper;

    @Override
    public PageResult<Brand> queryPage(QueryBrandDTO brandDTO) {
        Example example = new Example(Brand.class);
        // 开始分页查询
        PageHelper.startPage(brandDTO.getPage(), brandDTO.getRows());
        // 过滤
        if (StringUtils.isNotBlank(brandDTO.getKey())) {
            // 传入的key可以可能使名字也可以是首字母
            example.createCriteria()
                    .orLike("name","%"+brandDTO.getKey()+"%")
                    .orEqualTo("letter",brandDTO.getKey().toUpperCase());
        }
        // 排序
        if (StringUtils.isNotBlank(brandDTO.getSortBy())) {
            // 设置排序方式
            example.setOrderByClause(brandDTO.getSortBy()+ (brandDTO.getDesc() ?" desc":" asc"));
        }
        // 总页数
        List<Brand> brands = brandMapper.selectByExample(example);
        // 没有找到抛出异常
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOND);
        }
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        PageResult<Brand> brandPageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
        return brandPageResult;
    }

    @Override
    public boolean save(Brand brand, List<Long> cids) {
        // 将品牌插入到品牌表中

        if (brand==null) {
            throw new LyException(ExceptionEnum.BRAND_IS_NULL);
        }
        if (CollectionUtils.isEmpty(cids)) {
            throw new LyException(ExceptionEnum.CIDS_IS_NULL);
        }
        brand.setId(null);
        // 将传入的字符转成大写
        brand.setLetter(Character.toUpperCase(brand.getLetter()));
        int insert = brandMapper.insert(brand);
        // 找中间表中插入数据
        if (insert!=1) {
            throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
        }
        // 在中间表中插入数据
        for (Long cid : cids) {
            int cont = brandMapper.insertBrandCategory(brand.getId(), cid);
            if (cont!=1){
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }
        return true;
    }

    @Override
    @Transactional
    public void update(Brand brand, List<Long> cids) {
        // 修改数据
        brandMapper.updateByPrimaryKey(brand);
        // 先删除所有关于品牌的分类
        brandMapper.deleteBrandCategoryByBid(brand.getId());
        // 在新增
        for (Long cid : cids) {
            int cont = brandMapper.insertBrandCategory(brand.getId(), cid);
            if (cont!=1){
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }
    }

    @Override
    public boolean deleteById(Long id) {
        brandMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public Brand queryBrandByBid(Long brandId) {
        return brandMapper.selectByPrimaryKey(brandId);
    }

    @Override
    public List<Brand> queryBrandsByCid(Long cid) {
        return brandMapper.selectBrandsByCid(cid);
    }

    @Override
    public List<Brand> queryBrandsByBIds(Long... bids) {
        return brandMapper.selectByIdList(Arrays.asList(bids));
    }
}
